{
 "cells": [
  {
   "cell_type": "code",
   "id": "088825b3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T14:15:11.788911Z",
     "start_time": "2024-11-24T14:15:09.943748Z"
    }
   },
   "source": [
    "###标量、矢量、矩阵\n",
    "import torch\n",
    "\n",
    "x = torch.tensor(3.0)\n",
    "y = torch.tensor(2.0)\n",
    "\n",
    "x+y, x-y, x*y, x/y, x**y"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor(5.), tensor(1.), tensor(6.), tensor(1.5000), tensor(9.))"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 1
  },
  {
   "cell_type": "code",
   "id": "2ac688e7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T14:15:11.834689Z",
     "start_time": "2024-11-24T14:15:11.822722Z"
    }
   },
   "source": [
    "x = torch.arange(4, dtype=torch.float32)\n",
    "x[3]"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(3.)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 2
  },
  {
   "cell_type": "code",
   "id": "bba207b8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T14:15:11.928713Z",
     "start_time": "2024-11-24T14:15:11.914654Z"
    }
   },
   "source": [
    "len(x), x.shape"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4, torch.Size([4]))"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 3
  },
  {
   "cell_type": "code",
   "id": "5d678646",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T14:15:11.974493Z",
     "start_time": "2024-11-24T14:15:11.964521Z"
    }
   },
   "source": [
    "A = torch.arange(20).reshape(5,4)\n",
    "A"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0,  1,  2,  3],\n",
       "        [ 4,  5,  6,  7],\n",
       "        [ 8,  9, 10, 11],\n",
       "        [12, 13, 14, 15],\n",
       "        [16, 17, 18, 19]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  },
  {
   "cell_type": "code",
   "id": "5e951d2c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T14:15:12.547067Z",
     "start_time": "2024-11-24T14:15:12.533996Z"
    }
   },
   "source": [
    "A.T"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0,  4,  8, 12, 16],\n",
       "        [ 1,  5,  9, 13, 17],\n",
       "        [ 2,  6, 10, 14, 18],\n",
       "        [ 3,  7, 11, 15, 19]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 5
  },
  {
   "cell_type": "code",
   "id": "f71886f1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T14:15:12.640710Z",
     "start_time": "2024-11-24T14:15:12.632731Z"
    }
   },
   "source": [
    "B = torch.tensor([[1,2,3],\n",
    "                [2,0,4],\n",
    "                [3,4,5]])\n",
    "B == B.T"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[True, True, True],\n",
       "        [True, True, True],\n",
       "        [True, True, True]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 6
  },
  {
   "cell_type": "code",
   "id": "9834099d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T14:15:12.841882Z",
     "start_time": "2024-11-24T14:15:12.828261Z"
    }
   },
   "source": [
    "###张量，通用描述\n",
    "A = torch.arange(20,dtype=torch.float32).reshape(5,4)\n",
    "B = A.clone()\n",
    "A, A+B, A*B"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[ 0.,  1.,  2.,  3.],\n",
       "         [ 4.,  5.,  6.,  7.],\n",
       "         [ 8.,  9., 10., 11.],\n",
       "         [12., 13., 14., 15.],\n",
       "         [16., 17., 18., 19.]]),\n",
       " tensor([[ 0.,  2.,  4.,  6.],\n",
       "         [ 8., 10., 12., 14.],\n",
       "         [16., 18., 20., 22.],\n",
       "         [24., 26., 28., 30.],\n",
       "         [32., 34., 36., 38.]]),\n",
       " tensor([[  0.,   1.,   4.,   9.],\n",
       "         [ 16.,  25.,  36.,  49.],\n",
       "         [ 64.,  81., 100., 121.],\n",
       "         [144., 169., 196., 225.],\n",
       "         [256., 289., 324., 361.]]))"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 7
  },
  {
   "cell_type": "code",
   "id": "be7dadc9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T14:15:13.013342Z",
     "start_time": "2024-11-24T14:15:12.990812Z"
    }
   },
   "source": [
    "a = 2\n",
    "a+A, a*A"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[ 2.,  3.,  4.,  5.],\n",
       "         [ 6.,  7.,  8.,  9.],\n",
       "         [10., 11., 12., 13.],\n",
       "         [14., 15., 16., 17.],\n",
       "         [18., 19., 20., 21.]]),\n",
       " tensor([[ 0.,  2.,  4.,  6.],\n",
       "         [ 8., 10., 12., 14.],\n",
       "         [16., 18., 20., 22.],\n",
       "         [24., 26., 28., 30.],\n",
       "         [32., 34., 36., 38.]]))"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 8
  },
  {
   "cell_type": "code",
   "id": "54cb084e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T14:15:13.184484Z",
     "start_time": "2024-11-24T14:15:13.173513Z"
    }
   },
   "source": [
    "##########################降维\n",
    "A.sum()"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(190.)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 9
  },
  {
   "cell_type": "code",
   "id": "8cb7cd5d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T14:15:13.246906Z",
     "start_time": "2024-11-24T14:15:13.235819Z"
    }
   },
   "source": [
    "#指定轴降维，轴在输出中消失\n",
    "A, A.sum(axis=0), A.sum(axis=1)"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[ 0.,  1.,  2.,  3.],\n",
       "         [ 4.,  5.,  6.,  7.],\n",
       "         [ 8.,  9., 10., 11.],\n",
       "         [12., 13., 14., 15.],\n",
       "         [16., 17., 18., 19.]]),\n",
       " tensor([40., 45., 50., 55.]),\n",
       " tensor([ 6., 22., 38., 54., 70.]))"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 10
  },
  {
   "cell_type": "code",
   "id": "785fd0e3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T14:15:13.309062Z",
     "start_time": "2024-11-24T14:15:13.300086Z"
    }
   },
   "source": [
    "#沿所有轴降维，等于不写\n",
    "A.sum() ==A.sum(axis=[0,1])"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(True)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 11
  },
  {
   "cell_type": "code",
   "id": "454f6119",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T14:15:13.403138Z",
     "start_time": "2024-11-24T14:15:13.391045Z"
    }
   },
   "source": [
    "#平均值\n",
    "A, A.mean(), A.sum()/A.numel()"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[ 0.,  1.,  2.,  3.],\n",
       "         [ 4.,  5.,  6.,  7.],\n",
       "         [ 8.,  9., 10., 11.],\n",
       "         [12., 13., 14., 15.],\n",
       "         [16., 17., 18., 19.]]),\n",
       " tensor(9.5000),\n",
       " tensor(9.5000))"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 12
  },
  {
   "cell_type": "code",
   "id": "7c05b55e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T14:15:13.495547Z",
     "start_time": "2024-11-24T14:15:13.471612Z"
    }
   },
   "source": [
    "#计算平均值也可以降维\n",
    "A, A.mean(axis=0)"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[ 0.,  1.,  2.,  3.],\n",
       "         [ 4.,  5.,  6.,  7.],\n",
       "         [ 8.,  9., 10., 11.],\n",
       "         [12., 13., 14., 15.],\n",
       "         [16., 17., 18., 19.]]),\n",
       " tensor([ 8.,  9., 10., 11.]))"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 13
  },
  {
   "cell_type": "code",
   "id": "54d5cab3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T14:15:13.542421Z",
     "start_time": "2024-11-24T14:15:13.524471Z"
    }
   },
   "source": [
    "A.sum(axis = 0)/A.shape[0]"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 8.,  9., 10., 11.])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 14
  },
  {
   "cell_type": "code",
   "id": "cfe14406",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T14:15:13.588416Z",
     "start_time": "2024-11-24T14:15:13.566357Z"
    }
   },
   "source": [
    "A.mean(axis = 1), A.sum(axis=1)/A.shape[1]"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([ 1.5000,  5.5000,  9.5000, 13.5000, 17.5000]),\n",
       " tensor([ 1.5000,  5.5000,  9.5000, 13.5000, 17.5000]))"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 15
  },
  {
   "cell_type": "code",
   "id": "49de4157",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T14:15:13.665092Z",
     "start_time": "2024-11-24T14:15:13.643151Z"
    }
   },
   "source": [
    "###计算总和或者均值的时候，保持维度\n",
    "A, A.sum(axis=0, keepdims=True)"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[ 0.,  1.,  2.,  3.],\n",
       "         [ 4.,  5.,  6.,  7.],\n",
       "         [ 8.,  9., 10., 11.],\n",
       "         [12., 13., 14., 15.],\n",
       "         [16., 17., 18., 19.]]),\n",
       " tensor([[40., 45., 50., 55.]]))"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 16
  },
  {
   "cell_type": "code",
   "id": "179729d7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T14:15:13.711967Z",
     "start_time": "2024-11-24T14:15:13.691024Z"
    }
   },
   "source": [
    "A, A.sum(axis=0), A/A.sum(axis=0)"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[ 0.,  1.,  2.,  3.],\n",
       "         [ 4.,  5.,  6.,  7.],\n",
       "         [ 8.,  9., 10., 11.],\n",
       "         [12., 13., 14., 15.],\n",
       "         [16., 17., 18., 19.]]),\n",
       " tensor([40., 45., 50., 55.]),\n",
       " tensor([[0.0000, 0.0222, 0.0400, 0.0545],\n",
       "         [0.1000, 0.1111, 0.1200, 0.1273],\n",
       "         [0.2000, 0.2000, 0.2000, 0.2000],\n",
       "         [0.3000, 0.2889, 0.2800, 0.2727],\n",
       "         [0.4000, 0.3778, 0.3600, 0.3455]]))"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 17
  },
  {
   "cell_type": "code",
   "id": "cfbde5c8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T14:15:13.757938Z",
     "start_time": "2024-11-24T14:15:13.738897Z"
    }
   },
   "source": [
    "#保持维度除法好像与不保持维度除法得到的结果相同\n",
    "A_sum_keepdims=A.sum(axis=0,keepdims=True)\n",
    "A, A_sum_keepdims, A/A_sum_keepdims"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[ 0.,  1.,  2.,  3.],\n",
       "         [ 4.,  5.,  6.,  7.],\n",
       "         [ 8.,  9., 10., 11.],\n",
       "         [12., 13., 14., 15.],\n",
       "         [16., 17., 18., 19.]]),\n",
       " tensor([[40., 45., 50., 55.]]),\n",
       " tensor([[0.0000, 0.0222, 0.0400, 0.0545],\n",
       "         [0.1000, 0.1111, 0.1200, 0.1273],\n",
       "         [0.2000, 0.2000, 0.2000, 0.2000],\n",
       "         [0.3000, 0.2889, 0.2800, 0.2727],\n",
       "         [0.4000, 0.3778, 0.3600, 0.3455]]))"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 18
  },
  {
   "cell_type": "code",
   "id": "5d3c6b25",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T14:15:13.804721Z",
     "start_time": "2024-11-24T14:15:13.786768Z"
    }
   },
   "source": [
    "#点积 两个向量相同位置元素的乘机的和\n",
    "y = torch.ones(4, dtype=torch.float32)\n",
    "x, y, torch.dot(x,y)"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([0., 1., 2., 3.]), tensor([1., 1., 1., 1.]), tensor(6.))"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 19
  },
  {
   "cell_type": "code",
   "id": "0d877eff",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T14:15:13.851595Z",
     "start_time": "2024-11-24T14:15:13.829653Z"
    }
   },
   "source": [
    "torch.sum(x*y)"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(6.)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 20
  },
  {
   "cell_type": "code",
   "id": "d64ebfac",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T14:15:13.896475Z",
     "start_time": "2024-11-24T14:15:13.886502Z"
    }
   },
   "source": [
    "#矩阵向量积\n",
    "A = torch.arange(20, dtype=torch.float32).reshape(5,4)\n",
    "A, x, torch.mv(A,x)"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[ 0.,  1.,  2.,  3.],\n",
       "         [ 4.,  5.,  6.,  7.],\n",
       "         [ 8.,  9., 10., 11.],\n",
       "         [12., 13., 14., 15.],\n",
       "         [16., 17., 18., 19.]]),\n",
       " tensor([0., 1., 2., 3.]),\n",
       " tensor([ 14.,  38.,  62.,  86., 110.]))"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 21
  },
  {
   "cell_type": "code",
   "id": "c46ca133",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T14:15:13.943350Z",
     "start_time": "2024-11-24T14:15:13.918418Z"
    }
   },
   "source": [
    "#矩阵乘法 AB=A的行向量 * B的列向量\n",
    "B = torch.ones(12, dtype=torch.float32).reshape(4,3)\n",
    "A, B, torch.mm(A, B)"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[ 0.,  1.,  2.,  3.],\n",
       "         [ 4.,  5.,  6.,  7.],\n",
       "         [ 8.,  9., 10., 11.],\n",
       "         [12., 13., 14., 15.],\n",
       "         [16., 17., 18., 19.]]),\n",
       " tensor([[1., 1., 1.],\n",
       "         [1., 1., 1.],\n",
       "         [1., 1., 1.],\n",
       "         [1., 1., 1.]]),\n",
       " tensor([[ 6.,  6.,  6.],\n",
       "         [22., 22., 22.],\n",
       "         [38., 38., 38.],\n",
       "         [54., 54., 54.],\n",
       "         [70., 70., 70.]]))"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 22
  },
  {
   "cell_type": "code",
   "id": "b2300079",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T14:15:13.989225Z",
     "start_time": "2024-11-24T14:15:13.977259Z"
    }
   },
   "source": [
    "#张量的范数（norm）。范数是一个数学概念，它衡量向量或矩阵的大小。在机器学习和深度学习中，经常需要计算参数的范数来进行正则化或优化。\n",
    "#欧几里得距离是一个L2范数，表示为平方和的平方根\n",
    "u = torch.tensor([-3.0, 4.0])\n",
    "torch.norm(u), torch.norm(u, p=2)"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor(5.), tensor(5.))"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 23
  },
  {
   "cell_type": "code",
   "id": "ba593ae3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T14:15:14.035103Z",
     "start_time": "2024-11-24T14:15:14.024134Z"
    }
   },
   "source": [
    "#计算L1范数，表示为绝对值的和\n",
    "torch.norm(u, p=1),torch.abs(u).sum()"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor(7.), tensor(7.))"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 24
  },
  {
   "cell_type": "code",
   "id": "f290f6e8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T14:15:14.081978Z",
     "start_time": "2024-11-24T14:15:14.065024Z"
    }
   },
   "source": [
    "#计算矩阵的范数，元素平方和的平方根\n",
    "torch.norm(torch.ones(4,9))"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(6.)"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 25
  },
  {
   "cell_type": "code",
   "id": "3c298b57",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T14:15:14.127926Z",
     "start_time": "2024-11-24T14:15:14.114890Z"
    }
   },
   "source": [],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "id": "898a9f8f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T14:15:14.159864Z",
     "start_time": "2024-11-24T14:15:14.147802Z"
    }
   },
   "source": [],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "id": "6c277cdf",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T14:15:14.204649Z",
     "start_time": "2024-11-24T14:15:14.193678Z"
    }
   },
   "source": [
    "#一个矩阵A的转置的转置是A\n",
    "A == A.T.T"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[True, True, True, True],\n",
       "        [True, True, True, True],\n",
       "        [True, True, True, True],\n",
       "        [True, True, True, True],\n",
       "        [True, True, True, True]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 26
  },
  {
   "cell_type": "code",
   "id": "eca36412",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T14:15:14.280448Z",
     "start_time": "2024-11-24T14:15:14.268479Z"
    }
   },
   "source": [
    "#矩阵转置的和等于它们和的转置\n",
    "B = A.clone()\n",
    "(A+B).T == A.T+B.T"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[True, True, True, True, True],\n",
       "        [True, True, True, True, True],\n",
       "        [True, True, True, True, True],\n",
       "        [True, True, True, True, True]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 27
  },
  {
   "cell_type": "code",
   "id": "fb59cac3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T14:15:14.387576Z",
     "start_time": "2024-11-24T14:15:14.364929Z"
    }
   },
   "source": [
    "#对于任意方阵 A，其转置记作 A^T。A + A^T 一定总是对称的。\n",
    "\n",
    "A = torch.tensor([[1,2],\n",
    "                 [3,4]])\n",
    "A+A.T == (A+A.T).T "
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[True, True],\n",
       "        [True, True]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 28
  },
  {
   "cell_type": "code",
   "id": "1ebd71c4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T14:15:14.511825Z",
     "start_time": "2024-11-24T14:15:14.498708Z"
    }
   },
   "source": [
    "#len指的是第一个维度的len\n",
    "x = torch.arange(24).reshape(2,3,4)\n",
    "x, len(x)"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[[ 0,  1,  2,  3],\n",
       "          [ 4,  5,  6,  7],\n",
       "          [ 8,  9, 10, 11]],\n",
       " \n",
       "         [[12, 13, 14, 15],\n",
       "          [16, 17, 18, 19],\n",
       "          [20, 21, 22, 23]]]),\n",
       " 2)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 29
  },
  {
   "cell_type": "code",
   "id": "b7531f3f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T14:15:14.621147Z",
     "start_time": "2024-11-24T14:15:14.608925Z"
    }
   },
   "source": [
    "#行归一化\n",
    "A, A.sum(axis=1) ,A/A.sum(axis=1, keepdims=True) "
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[1, 2],\n",
       "         [3, 4]]),\n",
       " tensor([3, 7]),\n",
       " tensor([[0.3333, 0.6667],\n",
       "         [0.4286, 0.5714]]))"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 30
  },
  {
   "cell_type": "code",
   "id": "5b672f22",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T14:15:14.885840Z",
     "start_time": "2024-11-24T14:15:14.865180Z"
    }
   },
   "source": [
    "#对不同维度进行求和\n",
    "x, x.sum(axis=0),x.sum(axis=1), x.sum(axis=2)"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[[ 0,  1,  2,  3],\n",
       "          [ 4,  5,  6,  7],\n",
       "          [ 8,  9, 10, 11]],\n",
       " \n",
       "         [[12, 13, 14, 15],\n",
       "          [16, 17, 18, 19],\n",
       "          [20, 21, 22, 23]]]),\n",
       " tensor([[12, 14, 16, 18],\n",
       "         [20, 22, 24, 26],\n",
       "         [28, 30, 32, 34]]),\n",
       " tensor([[12, 15, 18, 21],\n",
       "         [48, 51, 54, 57]]),\n",
       " tensor([[ 6, 22, 38],\n",
       "         [54, 70, 86]]))"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 31
  },
  {
   "cell_type": "code",
   "id": "4c18fcf6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T14:19:27.090430Z",
     "start_time": "2024-11-24T14:19:27.069453Z"
    }
   },
   "source": [
    "#扩展：张量求norm\n",
    "# 创建一个形状为 (2, 3, 4) 的三维张量\n",
    "X = torch.randn((2, 3, 4))\n",
    "print(X)\n",
    "\n",
    "# 在不同轴上计算 Frobenius 范数\n",
    "frobenius_norm_all_axes = torch.linalg.norm(X)\n",
    "print(\"Frobenius Norm (across all axes):\", frobenius_norm_all_axes.item())\n",
    "\n",
    "# 在最后两个轴上计算 Frobenius 范数\n",
    "frobenius_norm_last_axes = torch.linalg.norm(X, dim=(-2, -1))\n",
    "print(\"Frobenius Norm (along last two axes):\", frobenius_norm_last_axes)\n",
    "\n",
    "# 在第一个轴上计算向量的 2-范数\n",
    "vector_norm = torch.linalg.norm(X, dim=0)\n",
    "print(\"Vector Norm (along the first axis):\", vector_norm)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[[ 0.1242, -0.7906, -0.2103,  0.0476],\n",
      "         [-1.1790,  0.0161, -0.6137,  0.7328],\n",
      "         [ 0.1254,  0.1474, -0.4531, -0.1466]],\n",
      "\n",
      "        [[ 0.6591, -0.1362,  1.6044,  0.6073],\n",
      "         [-1.1061, -0.4152, -0.8370,  1.6721],\n",
      "         [ 0.3878, -0.5226, -0.7456,  0.1263]]])\n",
      "Frobenius Norm (across all axes): 3.5409982204437256\n",
      "Frobenius Norm (along last two axes): tensor([1.8042, 3.0469])\n",
      "Vector Norm (along the first axis): tensor([[0.6707, 0.8023, 1.6181, 0.6091],\n",
      "        [1.6166, 0.4156, 1.0379, 1.8256],\n",
      "        [0.4075, 0.5430, 0.8725, 0.1934]])\n"
     ]
    }
   ],
   "execution_count": 35
  },
  {
   "cell_type": "code",
   "id": "dfb1215d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T14:15:15.088734Z",
     "start_time": "2024-11-24T14:15:15.083642Z"
    }
   },
   "source": [],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "id": "a1f4455a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-24T14:15:15.135601Z",
     "start_time": "2024-11-24T14:15:15.127622Z"
    }
   },
   "source": [],
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
